/***********************************
 *           3/24/2014             *
 *       by Meow dev (arm genesis) *
 *                                 *
 ***********************************/
package com.meow.dev.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.meow.dev.engine.UserData;
import com.meow.dev.engine.UserDataEncode;
import com.meow.dev.engine.UserLog;

public class LogToFile {
	private final static Logger LOGGER = Logger.getLogger(LogToFile.class.getName());
	public static String LOG_FILE_NAME = "userData_out.txt";
	public static String LOG_USER_FILE_NAME = "userLog_out.txt";
	
	private FileOutputStream fileOutputStream ;
	private FileInputStream fileInputStream ;
	
	public LogToFile() {
		// TODO Auto-generated constructor stub
	}
	
	
	public void writeLog( List<UserData> list){
		if(list.isEmpty()){
			LOGGER.info("#### Data Empty");
			return;
		}
		File file = new File(LOG_FILE_NAME);
		try {
			long start = System.currentTimeMillis();
			LOGGER.info(" *** start ***");
			fileOutputStream = new FileOutputStream(file);
			for( UserData u : list){
				fileOutputStream.write(u.toString().getBytes("utf-8"));
				fileOutputStream.write("\n".getBytes("utf-8"));
			}
			long end = System.currentTimeMillis();
			LOGGER.info(" *** finish in : " + (end - start) + " ms");
			
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.info("## Error " + e.toString());
		}finally{
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	public void writeUserLog( List<UserLog> list){
		if(list.isEmpty()){
			LOGGER.info("#### Data Empty");
			return;
		}
		File file = new File(LOG_USER_FILE_NAME);
		try {
			long start = System.currentTimeMillis();
			LOGGER.info(" *** start ***");
			fileOutputStream = new FileOutputStream(file);
			for( UserLog u : list){
				fileOutputStream.write(u.toString_1().getBytes("utf-8"));
				fileOutputStream.write("\n".getBytes("utf-8"));
			}
			long end = System.currentTimeMillis();
			LOGGER.info(" *** finish in : " + (end - start) + " ms");
			
		} catch (Exception e) {
			e.printStackTrace();
			LOGGER.info("## Error " + e.toString());
		}finally{
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	public List<UserData> readLog(String path){
		File file = new File(path);
		List<UserData>  list = new ArrayList<UserData>();
		
		BufferedReader br = null;
		try {
			long start = System.currentTimeMillis();
			LOGGER.info(" *** start ***");
			fileInputStream = new FileInputStream(file);
			br = new BufferedReader(new InputStreamReader(fileInputStream, Charset.forName("UTF-8")));
			String line;
			while ((line = br.readLine()) != null) {
				// convert to object
				UserData u = this.processToUserData(line);
				list.add(u);
			}
			
			long end = System.currentTimeMillis();
			LOGGER.info(" *** finish in : " + (end - start) + " ms");
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				br.close();
				fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return list;
	}
	
	public UserData processToUserData(String rawData){
//		enrollNumber=1, backupNumber=10, machinePrivilege=0, enableFlag=1, passwordAndCard=-1588155599, fingerData=0
		String[] header = rawData.split(",");
		LOGGER.info("## header size : " + header.length);
		UserData userData  = new UserData();
		
		String[] enrollNumber = header[0].split("=");
		userData.setEnrollNumber(toLong(enrollNumber[1]));

		String[] backupNumber = header[1].split("=");
		userData.setBackupNumber(toLong(backupNumber[1]));
		
		String[] machinePrivilege = header[2].split("=");
		userData.setMachinePrivilege(toLong(machinePrivilege[1]));
		
		String[] enableFlag = header[3].split("=");
		userData.setEnableFlag(toLong(enableFlag[1]));
		
		UserDataEncode dataEncode = new UserDataEncode();
		String[] passwordAndCard = header[4].split("=");
		dataEncode.setPasswordAndCard(toLong(passwordAndCard[1]));

		String[] fingerData = header[5].split("=");
//		System.out.println(fingerData[1]);
		String[] fingerDataArray = fingerData[1].split("#"); 
//		System.out.println("Length : " + fingerDataArray.length);
		if(fingerDataArray.length > 5){
			long[] d = new long[459];
			for(int i = 0 ; i < fingerDataArray.length ; i ++){
				d[i] = toLong(fingerDataArray[i]);
			}
			dataEncode.setFingerData(d);
		}
		userData.setDataEncode(dataEncode);
		
//		System.out.println(userData);
		
		return userData;
	}
	
	public long toLong( String v){
		long l = 0;
		try{
		 l =  Long.parseLong(v);
		}catch(Exception e){
			LOGGER.info("### " + e.toString());
		}
		return l;
	}
	
}
